package com.yami.shop.api.utils;


import java.util.Random;

/**
 *
 * 用字母和数字生成不重复的随机字符串
 *
 * @author lf
 * @date 2022-08-04
 *
 */
public class RandomUtil {

    /**
     * 字符源，可以剔除O、L、0和1，避免0和1与O和L混淆，这里没有剔除
     * 可以根据需要加入小写英文字母和特殊字符等
     */
    private static final String[] GENERATE_SOURCE = new String[]{"0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    private static final int STR_LEN = GENERATE_SOURCE.length;

    /**
     * 生成随机字符串，generateByRandom的简化版
     * @param count 随机字符串的长度
     * @return 随机字符串
     */
    public static String doGenerate(int count) {
        StringBuilder sb = new StringBuilder(count);
        for (int i = 0; i < count; i++) {
            final int index = new Random().nextInt(STR_LEN);
            sb.append(GENERATE_SOURCE[index]);
        }
        return sb.toString();
    }



}
